package com.smooth.mall.service.serviceImpl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.smooth.mall.exception.SmoothMallException;
import com.smooth.mall.exception.SmoothMallExceptionEnum;
import com.smooth.mall.model.dao.CategoryMapper;
import com.smooth.mall.model.pojo.Category;
import com.smooth.mall.request.AddCategoryReq;
import com.smooth.mall.service.CategoryService;

import com.smooth.mall.model.vo.CategoryVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;


@Service
public class CategoryServiceImpl implements CategoryService {

    @Autowired
    CategoryMapper categoryMapper;

    @Override
    public void add(AddCategoryReq addCategoryReq){
        Category category = new Category();
        //使用工具类来完成类的拷贝，替换原有的set方法
        BeanUtils.copyProperties(addCategoryReq,category);
        Category categoryOld= categoryMapper.selectByName(addCategoryReq.getName());
        if(categoryOld!=null){
            throw new SmoothMallException(SmoothMallExceptionEnum.NAME_EXISTED);
        }
        int count = categoryMapper.insertSelective(category);
        if(count==0){
            throw new SmoothMallException(SmoothMallExceptionEnum.CREATE_FALIED);
        }
    }

    @Override
    public void update(Category updateCategory){
        if(updateCategory.getName()!=null){
            Category categoryOld = categoryMapper.selectByName(updateCategory.getName());
            //判断获取的对象不为空，且获取的对象id与参数中的id不同，则名字冲突，抛出不允许重名异常
            if(categoryOld!=null&&!categoryOld.getId().equals(updateCategory.getId())){
                throw new SmoothMallException(SmoothMallExceptionEnum.NAME_EXISTED);
            }
        }
        int count = categoryMapper.updateByPrimaryKeySelective(updateCategory);
        if(count==0){
            throw new SmoothMallException(SmoothMallExceptionEnum.UPDATE_FAILED);
        }
    }


    @Override
    public void delete(Integer id){
        Category categoryOld = categoryMapper.selectByPrimaryKey(id);
        //查不到记录，无法删除，删除失败
        if(categoryOld==null){
            throw new SmoothMallException(SmoothMallExceptionEnum.DELETE_FAILED);
        }
        int count = categoryMapper.deleteByPrimaryKey(id);
        if(count==0){
            throw new SmoothMallException(SmoothMallExceptionEnum.DELETE_FAILED);
        }
    }

    @Override
    public PageInfo listForAdmin(Integer pageNumber, Integer pageSize){
        PageHelper.startPage(pageNumber,pageSize,"type,order_num");
        List<Category> categoryList = categoryMapper.selectList();
        PageInfo pageInfo = new PageInfo(categoryList);
        return pageInfo;
    }

    //客户端列表分页显示
    @Cacheable(value = "listForCustomer")
    @Override
    public List<CategoryVO> listForCustomer(Integer parentId) {
        ArrayList<CategoryVO> categoryVOList = new ArrayList<>();
//        Category category = new Category();
        recursiveFindCategories(categoryVOList,0);
        return categoryVOList;
    }
    //递归方法获取目录树
    private void recursiveFindCategories(List<CategoryVO> categoryVOList,Integer ParentId){
        //递归获取所有子类别，并组合成为一个“目录树”
        List<Category> categoryList = categoryMapper.selectCategoriesByParentId(ParentId);
        if(!CollectionUtils.isEmpty(categoryList)){
            for (int i = 0; i < categoryList.size(); i++) {
                Category category = categoryList.get(i);
                CategoryVO categoryVO = new CategoryVO();
                BeanUtils.copyProperties(category,categoryVO);
                categoryVOList.add(categoryVO);
                //给categoryVO设置childCategory属性
                recursiveFindCategories(categoryVO.getChildCategory(),categoryVO.getId());
            }
        }
    }
}



